/*
 * @ author madman 
 * @ use C端通话记录
 */
const CallrecordsModel = require('../../../models/nexmo/callrecords.model');
const Utils = require('../../utils');
class Callrecords {
    async add(ctx) {
      const _body = ctx.request.body
      const _params = Utils._params(_body)
      const result = await CallrecordsModel.create(_params);
      if (result) {
          return ctx.success({ msg: '添加成功' });
      } else {
          return ctx.error({ msg: '添加失败' });
      }
    }
    // 获取通话记录的反显
    async query(ctx) {
      const conid = ctx.query.conid
      const result = await CallrecordsModel.findOne({conversation_uuid: conid}, {_id: 0,msisdn: 1, to:1,dialingPrefix:1, countryName: 1});
      if (result) {
          return ctx.success({ msg: '查询成功', data: result});
      } else {
          return ctx.error({ msg: '查询失败' });
      }
  }
    async list(ctx) {
      const options = {
        name: 'nexmo/callrecords',
        type: 'list',
        findField: {}
    }
    var _res = {}
    if(ctx.query.server === 'true') {
        options.type = 'pop'
        options.populate = 'country'
        options.populateField = 'countryCode countryName monthPrice inPriceCost inPrice outPriceCost mobileOutPrice telOutPrice'
        if (ctx.query.to) {
           options.findField.to = new RegExp(ctx.query.to, 'i')
        }
    } else {
        options.findField.msisdn = ctx.query.msisdn
    }
    let data = await Utils.getPageList(ctx, options)
    _res = {
        data: data,
        msg: '查询成功'
    }
    if (ctx.query.server === 'true') {
        _res.total = data.total
        _res.pages = data.pages
    } 
    if (data) {
        return ctx.success(_res);
    }
    }
}

export default new Callrecords;